package idiom

import (
	"bufio"
	"os"
	"strings"
)

var idioms []string

func LoadIdioms(filename string) error {
	file, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		idioms = append(idioms, scanner.Text())
	}

	return scanner.Err()
}

func FindMatchingIdiom(lastChar string) (string, bool) {
	for _, idiom := range idioms {
		if strings.HasPrefix(idiom, lastChar) {
			return idiom, true
		}
	}
	return "", false
}

func IsValidIdiom(idiom string) bool {
	for _, i := range idioms {
		if i == idiom {
			return true
		}
	}
	return false
}
